﻿#include "stdafx.h"
#include "ScriptMapleWls.h"
#include "script.h"
#include "GameAppDlg.h"
#include "thread_control.h"
CScriptMapleWls::CScriptMapleWls()
{
}


CScriptMapleWls::~CScriptMapleWls()
{
}

bool CScriptMapleWls::bossAttack()
{
	long x, y, k = 0,j=0;
	for (size_t i = 0; i < 15; i++)
	{
		//g_main_cwnd->AppendText(g_info[index].dm->GetColor(137, 13));
		if (m_dm->RunScript(L"乌鲁斯结束")) {
			return true;
		}
		for (size_t i = 0; i < 5; i++)
		{
			m_dm->FindColorBlock(0, 228, 1024, 580, L"292222-101010", 1, 2000, 150, 50, &x, &y);
			if (g_info[m_DmIndex].x > 0) {
				x = g_info[m_DmIndex].x;
				break;
			}
			if (x > 0) {
				break;
			}

			ScriptDelay(m_DmIndex, 100);
		}
		if (x > 521 && x < 850)
		{
			//往右直接攻击
			k = 1;
			//OutPutListView(taskIndex, L"往右直接攻击,坐标:" + NumberToString(x));
			Attack(39);
			i--;
			continue;
		}
		else if (x > 850) {
			k = 1;
			//OutPutListView(taskIndex, L"往右移动攻击,坐标:" + NumberToString(x));
			MoveAttack( 39);
			i--;
			continue;
		}
		else if (x > 150 && x < 500) {
			k = 0;
			//OutPutListView(taskIndex, L"往左攻击,坐标:" + NumberToString(x));
			Attack(7);
			i--;
			continue;
		}
		else if (x < 150 && x>0) {
			k = 0;
			//OutPutListView(taskIndex, L"往左移动攻击,坐标:" + NumberToString(x));
			MoveAttack( 37);
			i--;
			continue;
		}
		if (k == 1) {
			i = 8;
			k = 0;
		}
		if (i <= 7) {

			//OutPutListView(taskIndex, L"往左跳");
			JumpAttack( 37, g_info[m_DmIndex].key);
		}
		else if (i < 15 && i>7) {
			//OutPutListView(taskIndex, L"往右跳");
			JumpAttack( 39, g_info[m_DmIndex].key);
		}
	}
	return false;
}

//开始攻击
void CScriptMapleWls::Attack(int key)
{
	long x, y;
	m_dm->KeyPress(key);
	m_dm->delay(100);
	m_dm->KeyPress(key);
	m_dm->delay(100);
	m_dm->KeyUp(key);
	for (size_t i = 0; i < 5; i++)
	{
		ScriptDelay(m_DmIndex, 200);
		m_dm->KeyDown(16);
		m_dm->delay(200);
		m_dm->KeyUp(16);
		ScriptDelay(m_DmIndex, 200);
		m_dm->FindColorBlock(0, 228, 1024, 580, L"292222-101010", 1, 3000, 150, 50, &x, &y);
		g_info[m_DmIndex].x = x;
	}
}

//移动攻击
void CScriptMapleWls::MoveAttack(int key)
{
	long x, y;
	m_dm->KeyDown(key);
	m_dm->delay(2000);
	m_dm->KeyUp(key);
	m_dm->delay(500);
	for (size_t i = 0; i < 5; i++)
	{
		ScriptDelay(m_DmIndex, 200);
		m_dm->KeyDown(16);
		m_dm->delay(200);
		m_dm->KeyUp(16);
		ScriptDelay(m_DmIndex, 200);
		m_dm->FindColorBlock(0, 228, 1024, 500, L"292222-101010", 1, 3000, 150, 50, &x, &y);
		g_info[m_DmIndex].x = x;
	};
}

//跳
void CScriptMapleWls::JumpAttack(int key, int mode)
{
	long x, y;
	m_dm->KeyDown(key);
	m_dm->delay(100);
	m_dm->KeyPress(mode);//18alt跳 17 ctrl
	m_dm->delay(100);
	m_dm->KeyPress(mode);//18alt跳 17 ctrl
	m_dm->KeyUp(key);
	m_dm->delay(500);
	m_dm->FindColorBlock(0, 228, 1024, 580, L"292222-101010", 1, 3000, 150, 50, &x, &y);
	g_info[m_DmIndex].x = x;
}

//开始
void CScriptMapleWls::DoWork()
{
	if (this->LoginOpenClient()) {
		if (this->LoginGame()) {
			TaskRun();
		}
	}
}

//运行任务
void CScriptMapleWls::TaskRun()
{
	CString strList = L"关闭广告|关闭广告1|关闭广告2|登录确定";
	g_info[m_DmIndex].key = 18;
	long x, y;
	bool bFindComputer = false;
	Sleep(2000);
	//long hwnd = 0;
	while (m_Flag)
	{
		if (!FindHwnd()) {
			m_Flag = false;
		}
		if (!bFindComputer) {
			for (size_t i = 0; i < 5; i++)
			{
				RunScriptList( strList);
				bFindComputer = FindComputer();
				if (bFindComputer) {
					break;
				}
			}
			bFindComputer = true;
		}
		RunScriptList(strList);

		//关闭组队
		CloseTearm();
		//位置在交易地图则退出交易地图
		if (m_dm->RunScript(L"交易地图")) {
			m_dm->KeyPressChar(L"up");
		}
		//执行进入wls操作
		if (m_dm->RunScript(L"冰峰雪域地图") || m_dm->RunScript(L"导航按钮")) {
			ChoseBoss(L"选中乌鲁斯", 92, 260);//选择乌鲁斯
		}
		//退出扎昆
		ExitZkjt();
		ExitZkrk();
		
		ShowLog(L"当前状态:"+ g_AccountFile.GetString(NumberToString(m_Index + 1), L"state")+L"当前索引:"+NumberToString(m_Index));
		if (m_State == 0 && bFindComputer) {

			if (m_dm->RunScript(L"乌鲁斯地图")) {
				ShowLog(L"查找npc");
				if (m_dm->RunScript(L"乌鲁斯") || m_dm->RunScript(L"乌鲁斯1") || m_dm->RunScript(L"乌鲁斯2")) {
					for (size_t i = 0; i < 5; i++)
					{
						ShowLog(L"查找选项");
						m_dm->RunScript(L"乌鲁斯选择");
						ScriptDelay(m_DmIndex, RandomNumber(300, 500));
						if (m_dm->RunScript(L"乌鲁斯下一步")) {
							m_dm->KeyPressChar(L"space");
							ScriptDelay(m_DmIndex, RandomNumber(100, 200));
						}
						if (m_dm->RunScript(L"乌鲁斯下一步1")) {
							m_dm->KeyPressChar(L"space");
							ScriptDelay(m_DmIndex, RandomNumber(100, 200));
						}
						if (m_dm->RunScript(L"退出队伍窗口")) {
							m_dm->KeyPressChar(L"space");
							ScriptDelay(m_DmIndex, RandomNumber(500, 800));
							CloseTearm();
						}
						int num = m_dm->GetColorNum(66, 573, 510, 749, L"44ddff", 1);
						ShowLog(L"下一步数量:" + NumberToString(num));
						if (num > 1000) {
							m_dm->KeyPressChar(L"space");
						}
					}
				}
				else
				{
					m_dm->KeyDownChar(L"right");
					ScriptDelay(m_DmIndex, RandomNumber(3000, 3500));
					m_dm->KeyUpChar(L"right");
				}
				if (m_dm->RunScript(L"等待组队")) {
					ScriptDelay(m_DmIndex, RandomNumber(1000, 1500));
					m_dm->MoveTo(RandomNumber(200, 1000), RandomNumber(1, 400));
					bool bExitwls = false;
					//g_bloodState = true;
					int k = 1;
					while (m_Flag)
					{
						if (!FindHwnd()) {
							m_Flag = false;
						}
						m_dm->RunScript(L"乌鲁斯选择");
						if (m_dm->RunScript(L"乌鲁斯下一步")) {
							m_dm->KeyPressChar(L"space");
							ScriptDelay(m_DmIndex, RandomNumber(100, 200));
						}
						if (m_dm->RunScript(L"乌鲁斯下一步1")) {
							m_dm->KeyPressChar(L"space");
							ScriptDelay(m_DmIndex, RandomNumber(100, 200));
						}
						if (k > 100) {
							m_dm->MoveTo(RandomNumber(200, 1000), RandomNumber(50, 300));
							k = 1;
						}
						ShowLog(L"进入等待环节");
						m_dm->RunScript(L"等待组队");
						if (m_dm->RunScript(L"组队确定")) {
							ScriptDelay(m_DmIndex, RandomNumber(200, 300));
							m_dm->MoveTo(RandomNumber(200, 1000), RandomNumber(1, 300));
						}
						if (m_dm->RunScript(L"组队取消")) {
							ScriptDelay(m_DmIndex, RandomNumber(200, 300));
							m_dm->MoveTo(RandomNumber(200, 1000), RandomNumber(1, 300));
						}
						if (m_dm->RunScript(L"进入乌鲁斯")) {
							m_BloodState = true;
							bExitwls = bossAttack();
						}
						if (m_dm->RunScript(L"乌鲁斯结束") || bExitwls) {
							m_BloodState = false;
							Sleep(9000);
							ExitWls();
							break;
						}
						k++;
					}
				}
			}
		}

		if (m_dm->RunScript(L"乌鲁斯结束1")) {
			//g_bloodState = false;
			bFindComputer = false;
			ShowLog(L"乌鲁斯结束");
			m_dm->KeyPressChar(L"esc");
			Sleep(300);
			for (size_t i = 0; i < 5; i++)
			{
				if (m_dm->RunScript(L"乌鲁斯结束1")) {
					Sleep(100);
					m_dm->KeyPressChar(L"esc");
					break;
				}
				Sleep(100);
			}
			g_main_cwnd->m_list_info.SetItemText(m_Index, 6, L"完成");//当前账号置完成
			m_Index = m_Index + 1;
			g_AccountFile.SetString(NumberToString(m_Index), L"state", L"1");
			int nLineCode = g_IniFile.GetInt(L"Set", L"CodeLine");//当前配置线路
			int nLineCode1 = _wtoi(g_main_cwnd->m_list_info.GetItemText(m_Index, 12));//下一条线路
			if (nLineCode == nLineCode1) {
				//切换角色
				m_State = 0;
				SwicthRole();
				
			}
			else {
				m_Flag = false;
				m_State = 0;
				return;
			}
		}
	}
	ShowLog(L"任务执行完毕");
	//return false;
}

void CScriptMapleWls::CheckGameState()
{
	
	if (m_BloodState) {
		if (m_SubDm->GetColor(545, 712) == L"f0f0f0") {
			if (m_SubDm->GetColor(567, 720) == L"9e9da0") {
				//g_main_cwnd->AppendText(L"补血");
				m_SubDm->KeyPress(35);
			}
		}
		else {
			if (m_SubDm->GetColor(567, 736) == L"9e9da0") {
				g_main_cwnd->AppendText(L"补血");
				m_SubDm->KeyPress(35);
			}
		}
		Sleep(1000);
	}
}



bool CScriptMapleWls::WlsYouPiao() {
	//1.是否在乌鲁斯地图
	//2.查找npc 位置
	CString strList = L"关闭广告|关闭广告1|关闭广告2|登录确定";
	long x, y;
	//CDm *dm = g_info[index].dm;
	bool bDrop = false;
	while (true) {
		RunScriptList( strList);
		//关闭组队
		CloseTearm();
		//位置在交易地图则退出交易地图
		if (m_dm->RunScript(L"交易地图")) {
			m_dm->KeyPressChar(L"up");
		}
		//执行进入wls操作
		if (m_dm->RunScript(L"冰峰雪域地图") || m_dm->RunScript(L"导航按钮")) {
			ChoseBoss(L"选中乌鲁斯", 92, 260);//选择乌鲁斯
		}
		//退出扎昆
		ExitZkjt();
		ExitZkrk();

		//材料窗口是否打开
		if (m_dm->RunScript(L"乌鲁斯材料窗口")) {
			for (size_t i = 0; i < 100; i++)
			{
				Sleep(RandomNumber(500, 1000));
				m_dm->RunScript(L"乌鲁斯换取");
				Sleep(RandomNumber(500, 1000));
				if (m_dm->RunScript(L"确定")) {
					m_dm->KeyPressChar(L"enter");
					Sleep(RandomNumber(1500, 2000));
				}


				if (m_dm->RunScript(L"乌鲁斯邮票结束")) {
					for (size_t j = 0; j < 6; j++)
					{
						m_dm->RunScript(L"快捷卡片");
						if (m_dm->RunScript(L"下一步1")) {
							//m_dm->KeyPressChar(L"enter");
							m_dm->KeyPress(32);
							Sleep(RandomNumber(1500, 2000));
						}
					}
					
					g_main_cwnd->m_list_info.SetItemText(m_Index, 6, L"完成");//当前账号置完成
					m_Index = m_Index + 1;
					g_AccountFile.SetString(NumberToString(m_Index), L"state", L"1");
					int nLineCode = g_IniFile.GetInt(L"Set", L"CodeLine");//当前配置线路
					int nLineCode1 = _wtoi(g_main_cwnd->m_list_info.GetItemText(m_Index, 12));
					if (nLineCode == nLineCode1) {
						//切换角色
						bDrop = false;
						
						SwicthRole();
						break;
					}
					else {
						bDrop = false;
						return true;
					}
					//关闭窗口
				}
				if (m_dm->RunScript(L"下一步1")) {
					//m_dm->KeyPressChar(L"enter");
					m_dm->KeyPress(32);
					for (size_t j = 0; j < 10; j++)
					{
						if (!m_dm->RunScript(L"下一步1")) {
							Sleep(RandomNumber(500, 1000));
							break;
						}
						Sleep(RandomNumber(50, 100));
					}
					if (!bDrop) {//首次检查快捷蓝是否存在邮票
						for (size_t k = 0; k < 5; k++)
						{
							if (!m_dm->RunScript(L"快捷卡片")) {
								m_dm->KeyPressChar(L"i");
								Sleep(RandomNumber(1500, 2000));
								if (m_dm->RunScript(L"背包线", &x, &y)) {
									m_dm->Click(x + 41, y + 3, 5, 5);
									Sleep(RandomNumber(1500, 2000));
									if (m_dm->RunScript(L"邮票卡片", &x, &y)) {
										//m_dm->Click(x, y, 10, 5);
										m_dm->MoveToEx(x, y, 10, 5);
										Sleep(RandomNumber(1500, 2000));
										m_dm->LeftClick();
										Sleep(RandomNumber(1500, 2000));
										m_dm->MoveTo(890 + RandomNumber(1, 20), 698 + RandomNumber(5, 10));
										Sleep(RandomNumber(1500, 2000));
										m_dm->LeftDoubleClick();
										Sleep(RandomNumber(1500, 2000));
										m_dm->MoveTo(790 + RandomNumber(5, 50), 698 + RandomNumber(5, 10));
										Sleep(RandomNumber(1500, 2000));
										if (m_dm->RunScript(L"背包线")) {
											m_dm->KeyPressChar(L"i");
										}
									}
								}
							}
							else
							{
								bDrop = true;
								break;
								//找到卡片 是否
							}
						}
					}


				}


				if (!m_dm->RunScript(L"快捷邮票0")) {
					if (m_dm->RunScript(L"快捷卡片")) {
						m_dm->KeyPress(45);
						Sleep(RandomNumber(500, 800));
						if (!m_dm->RunScript(L"快捷邮票0")) {
							m_dm->KeyPress(45);
							Sleep(RandomNumber(200, 300));
						}
					}
				}



			}
		}
		else {
			m_dm->RunScript(L"npc乌鲁斯材料");
			Sleep(RandomNumber(500, 1000));
			for (size_t i = 0; i < 5; i++)
			{
				if (m_dm->RunScript(L"下一步1")) {
					//m_dm->KeyPressChar(L"enter");
					m_dm->KeyPress(32);
					Sleep(RandomNumber(1500, 2000));
				}
			}

		}
		//Sleep(RandomNumber(500, 1000));
	}
	return true;

}

void CScriptMapleWls::DoWorkYou()
{
	if (this->LoginOpenClient()) {
		if (this->LoginGame()) {
			WlsYouPiao();
		}
	}
}



